//
//  FMtypeViewController.m
//  FunKey
//
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//
// 二级页面--->



#import "FMtypeViewController.h"
#import "FMPersonVC.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
@interface FMtypeViewController ()


@property (retain ,nonatomic) MBProgressHUD *MBP;

@end

@implementation FMtypeViewController

-(void)dealloc
{
    [_tableView release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    
    
    NSString *title = [self.name stringByReplacingOccurrencesOfString:@"|" withString:@""];
    self.navigationItem.title = title;
    
    
    UIImage *image = [UIImage imageNamed:@"player2.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(player1)];
    
    
    
    UIImage *image2 = [UIImage imageNamed:@"VList_Left@2x.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.hidesBottomBarWhenPushed = YES;//取消tabBar
    
    
    [self createTableView];
    [self getMassage];
    [self addHeader];//下拉刷新
   
    
  
    

}

-(void)createTableView
{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MYWIDTH, 618 * HEIGHT)];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor blackColor];
    [self.tableView release];
    
     
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.showsVerticalScrollIndicator = NO;//取消纵向滑条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell之间的线
    
    //菊花(小黄人)
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    

}





-(void)addHeader
{
    
    [self.tableView addHeaderWithCallback:^{
        
   
        
        [self getMassage];
        
        
        
    }];
    

}




#pragma mark ---进入播放页面
-(void)player1
{
    FMPlayerVC *playerVC = [FMPlayerVC shareHandleController];
    if (playerVC.trackId == nil) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您当前没有听电台哟~~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        return;
        
    }

    [self.navigationController pushViewController:playerVC animated:YES];
}


-(void)getMassage
{
    
    NSString *utf8Str = self.name;
    NSLog(@"name = %@",self.name);
   //字符串转成UTF8格式
    NSString *dataUTF8 = [utf8Str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


    NSString *string =[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=hot&device=iPhone&page=1&per_page=20&status=0&tag_name=%@",dataUTF8];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    

    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
 
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"list"];
        
        self.array = [NSMutableArray array];
        for (NSMutableDictionary *smallDic  in array) {
            FMTypeModel *typeModel = [[FMTypeModel alloc] init];
            typeModel.myId = [smallDic objectForKey:@"id"];
            typeModel.albumCoverUrl290 = [smallDic objectForKey:@"albumCoverUrl290"];
            typeModel.title = [smallDic objectForKey:@"title"];
            typeModel.playsCounts = [NSString stringWithFormat:@"%@",[smallDic objectForKey:@"playsCounts"]];
            [self.array addObject:typeModel];
            [typeModel release];
            
        }
        //刷新tableView
        [self.tableView reloadData];
        [GiFHUD dismiss];//菊花隐藏
        [self.tableView  headerEndRefreshing];//结束下拉
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       // [GiFHUD dismiss];//菊花隐藏
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        NSLog(@"失败==== %@",error);
        
    }];
}


//区数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}

//每个区cell的数量
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count  ;
    
}


//每个cell上内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellName = @"myCell";
    FMTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[FMTypeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        
    }
  
        FMTypeModel *model = [self.array objectAtIndex:indexPath.row];
        cell.TypeModel = model;
    
    
    //渐变效果
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0.1;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1.5];//时间
    cell.alpha = 1;
    [UIView commitAnimations];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消点击效果
   

    return  cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}


#pragma mark -- 点击进入个人的分类页面:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    if ([DataHandle shareHandleController].monitorNetwork == NO) {
//        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
//        
//        return;
//    }

    
    
    FMPersonVC *FMPerson = [[FMPersonVC alloc]init];
    FMPerson.hidesBottomBarWhenPushed = YES;//隐藏下方Bar
    FMTypeModel *model = [self.array objectAtIndex:indexPath.row];
//    NSLog(@"FmTypeId == %@",model.myId);
    FMPerson.name = model.title;
    FMPerson.myId = model.myId;
    [self.navigationController pushViewController:FMPerson animated:YES];
    [FMPerson release];
    
    
    
}


-(void)back
{
    [GiFHUD dismiss];//菊花隐藏
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
